package com.qc31.monitorlib.ui.carmonitor

import autodispose2.autoDispose
import com.qc31.baselib.auto.AutoDisposeViewModel
import com.qc31.comlib.TimeUtil
import com.qc31.comlib.UrlUtil
import com.qc31.comlib.UserConstant
import com.qc31.comlib.entity.HistoryVideo
import com.qc31.comlib.net.ServiceHelper
import io.reactivex.rxjava3.subjects.PublishSubject
import java.net.URLEncoder
import java.util.*

/**
 *@Author Jasper
 *@Time   2020/9/30
 *@Desc   todo
 */
class TmnVideoViewModel : AutoDisposeViewModel() {
    //播放类型
    var videoState = 0
    var carId = ""
    var uuid = ""
    var multiple = 1
    var seekToTime = ""
    var url: String = ""
    var videoEntity: HistoryVideo.Result? = null
    val videoSub: PublishSubject<Int>
            by lazy { PublishSubject.create<Int>() }
    val videoToast: PublishSubject<String>
            by lazy { PublishSubject.create<String>() }


    fun stopPlay() {
        videoState = 0
        operateVideo()
    }

    fun pauseVideo() {
        videoState = 1
        operateVideo()
    }

    fun resumeVideo() {
        videoState = 5
        operateVideo()
    }

    fun fastVideo() {
        videoState = 2
        operateVideo()
    }

    fun frameVideo() {
        videoState = 4
        operateVideo()
    }

    fun slowVideo() {
        videoState = 3
        operateVideo()
    }


    fun multipleVideo(multiple: Int) {
        this.multiple = multiple
        operateVideo()
    }

    /**
     * 视频URL
     */
    fun playParam() = videoEntity?.run {
        uuid = UUID.randomUUID().toString()
        "${UrlUtil.mVideoHost}/httpflv.hplay.$uuid.${
                URLEncoder.encode(UserConstant.current_userId, "utf-8")
        }.$carId.$channel.$avType.$streamType.$storeType.0.1.$startTime.$endTime"

    } ?: ""


    //    private fun operateParam(): String {
//        if (uuid.isEmpty()) return ""
//        val baseParam = "phone.hctr.$uuid.$carId.${videoEntity?.channel}"
//        when (videoState) {
//            //关闭
//            0 -> return "$baseParam.2.0.000000000000"
//            //暂停
//            1 -> return "$baseParam.1.0.000000000000"
//            //快进
//            2 -> return "$baseParam.3.$multiple.000000000000"
//            //快退
//            3 -> return "$baseParam.4.$multiple.000000000000"
//            //关键帧播放
//            4 -> return "$baseParam.6.0.000000000000"
//            //恢复播放
//            5 -> return "$baseParam.0.0.000000000000"
//            //拖动播放
//            else -> return "$baseParam.5.0.$seekToTime"// 时间差后计算时间格式yymmddhhmmss
//        }
//    }
    private fun operateParam(): String {
        if (uuid.isEmpty()) return ""
        val baseParam = "httpflv.hctr"

        when (videoState) {
            //关闭
            0 -> return "$baseParam.close.$uuid"
            //暂停
            1 -> return "$baseParam.pause.$uuid"
            //快进
            2 -> return "$baseParam.seek.$uuid.$multiple."
            //快退
            3 -> return "$baseParam.back.$uuid.$multiple"
            //关键帧播放
            4 -> return "$baseParam.back.$uuid"
            //恢复播放
            5 -> return "$baseParam.resume.$uuid"
            //拖动播放
            else -> return "$baseParam.seek.$uuid.$seekToTime"// 时间差后计算时间格式yymmddhhmmss
        }
    }

    /**
     * 拖动播放
     */
    fun endTime(progress: Long) {
        videoState = 6
        seekToTime = TimeUtil.instance.getAddLockTime(videoEntity?.startTime ?: "", progress)
        operateVideo()
    }

    private fun operateVideo() {
        val operateParam = operateParam()
        if (operateParam.isEmpty()) return
        ServiceHelper.getRepository().getVideoState(operateParam)
            .autoDispose(this)
            .subscribe({
                if (it == "0") {
                    videoSub.onNext(videoState)
                    return@subscribe
                }
                videoToast.onNext(it)
            }, {
                if (videoState != 0)
                    videoToast.onNext("66")
            })

    }

    fun getErrorState() {
        ServiceHelper.getRepository().getVideoState(getVideoState("httpflv.state.$uuid"))
            .autoDispose(this).subscribe({
                videoToast.onNext(it)
            }, {
                if (videoState != 0)
                    videoToast.onNext("66")
            })
    }

    /**
     * 播放状态
     */
    private fun getVideoState(uuid: String): String {
        return "httpflv.state.$uuid"
    }
}
